package com.hhh.sun.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhh.sun.mapper.TbOrderMapper;
import com.hhh.sun.pojo.entity.TbCharging;
import com.hhh.sun.pojo.entity.TbOrder;
import com.hhh.sun.pojo.entity.TbUser;
import com.hhh.sun.service.ChargingService;
import com.hhh.sun.service.OrderService;
import com.hhh.sun.service.UserService;
import com.hhh.sun.vo.req.OrderAddVo;
import com.hhh.sun.vo.resp.LoginRespVo;
import com.hhh.sun.vo.resp.OrderResVo;
import com.hhh.sun.vo.resp.R;
import com.hhh.sun.vo.resp.ResponseCode;
import org.joda.time.DateTime;
import org.joda.time.Minutes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<TbOrderMapper, TbOrder>implements OrderService {
    @Autowired
    private UserService userService;
    @Autowired
    private ChargingService chargingService;
    @Autowired
    private TbOrderMapper orderMapper;
    /**
     * 查询一个用户的所有订单
     */
    @Override
    public R<List<TbOrder>> getInfoByUserName(String userName) {
        TbUser user = userService.getOne(Wrappers.<TbUser>lambdaQuery().eq(TbUser::getUserName, userName));
        if(user.getIsban()==1){
            return R.error(ResponseCode.USER_IS_BAN);
        }
        Integer userId = user.getId();
        List<TbOrder>orders= orderMapper.selectAllOrderInfoByUserName(userId);
        if(orders==null){
            return R.error(ResponseCode.NO_RESPONSE_DATA);
        }
        return R.ok(orders);
    }

    /**
     * 预约订单
     */
    @Override
    public R<OrderResVo> preview(OrderAddVo vo) {
        //根据用户名找用户id
        TbUser user = userService.getOne(Wrappers.<TbUser>lambdaQuery().eq(TbUser::getUserName, vo.getUserName()));
        if(user.getIsban()==1){
            return R.error(ResponseCode.USER_IS_BAN);
        }
        Integer userId = user.getId();
        //根据充电站名字找充电站id
        TbCharging charging = chargingService.getOne(Wrappers.<TbCharging>lambdaQuery().eq(TbCharging::getChargingName, vo.getChargingName()));
        if(charging.getIsenabled()==0){
            return R.error(ResponseCode.CHARGING_IS_DISABLED);
        }
        Integer chargingId = charging.getId();
        //封装订单entity对象
        TbOrder order = TbOrder.builder().userId(userId)
                .chargingId(chargingId)
                .beginTime(vo.getBeginTime())
                .lastTime(vo.getLastTime())
                .build();
        int i = orderMapper.insert(order);
        if(i==0){
            return R.error(ResponseCode.ERROR);
        }
        OrderResVo result = OrderResVo.builder().orderId(order.getId())
                .beginTime(vo.getBeginTime())
                .lastTime(vo.getLastTime())
                .build();
        return R.ok(result);
    }

    /**
     * 开始充电
     */
    @Override
    public R<Object> start(Integer orderId) {
        TbOrder order = orderMapper.selectById(orderId);
        DateTime beginTime = new DateTime(order.getBeginTime());

        DateTime nowTime = DateTime.now();
        //获取两个时间的分钟差
        int minutes = Minutes.minutesBetween(beginTime, nowTime).getMinutes();
        if(minutes>30){//30分钟内为进行充电，则超时
            orderMapper.update(null,Wrappers.<TbOrder>lambdaUpdate().eq(TbOrder::getId,orderId).set(TbOrder::getStatus,-1));
            return R.error(ResponseCode.TIME_OUT);
        }
        int i = orderMapper.update(null, Wrappers.<TbOrder>lambdaUpdate().eq(TbOrder::getId, orderId).set(TbOrder::getStatus, 1));
        return i==0?R.error(ResponseCode.ERROR):R.ok(ResponseCode.SUCCESS);

    }
}
